<template>
  <div class="mainBox" v-if="list?.length!==0">
    <div class="avatar" v-for="item in list " :key="item.commentId">
      <el-avatar :size="50" :src="item.user?.avatar?item.user?.avatar:circleUrl"></el-avatar>
      <div class="right">
        <!-- 父级评论 -->
        <div class="name">
          <div class="name_1">{{item.user.username}}</div>
          <div v-if="item.user.userId === $store.state.moment.userId" class="tag"><i class="svg-icon up-web up-icon" data-v-7592db79=""><svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <rect x="3" y="6" width="18" height="11.5" rx="2" fill="#FF6699"></rect>
                <path d="M5.7 8.36V12.79C5.7 13.72 5.96 14.43 6.49 14.93C6.99 15.4 7.72 15.64 8.67 15.64C9.61 15.64 10.34 15.4 10.86 14.92C11.38 14.43 11.64 13.72 11.64 12.79V8.36H10.47V12.81C10.47 13.43 10.32 13.88 10.04 14.18C9.75 14.47 9.29 14.62 8.67 14.62C8.04 14.62 7.58 14.47 7.3 14.18C7.01 13.88 6.87 13.43 6.87 12.81V8.36H5.7ZM13.0438 8.36V15.5H14.2138V12.76H15.9838C17.7238 12.76 18.5938 12.02 18.5938 10.55C18.5938 9.09 17.7238 8.36 16.0038 8.36H13.0438ZM14.2138 9.36H15.9138C16.4238 9.36 16.8038 9.45 17.0438 9.64C17.2838 9.82 17.4138 10.12 17.4138 10.55C17.4138 10.98 17.2938 11.29 17.0538 11.48C16.8138 11.66 16.4338 11.76 15.9138 11.76H14.2138V9.36Z" fill="white"></path>
              </svg></i></div>
        </div>
        <div class="content" v-html="item.content"></div>
        <div class="footer">
          <div class="time">{{ item.commentTime }}
            <span class="reply" @click="shoReply(item)">回复</span></div>
        </div>
        <!-- 子级评论 -->
        <div class="sonComment" v-for="child in item.childrenList">
          <div class="left">
            <el-avatar :size="20" :src="child.user?.avatar?child.user?.avatar:circleUrl"></el-avatar>
          </div>
          <div class="right">
            <div class="name_1">{{child.user?.username?child.user?.username:'神秘用户'}} <span class="huifu_text"> 回复 <span class="ai_name">@{{child.replyUser?.username?child.replyUser?.username:'神秘用户'}}</span></span></div>
            <div class="text">{{ child.content }}</div>
            <div class="footer">
              <div class="time">{{child.commentTime}} <span class="reply" @click=" shoReply(item,child)">回复</span></div>
            </div>
          </div>
        </div>
        <div class="callReply" v-show="isShow && commentId === item.commentId">
          <el-avatar :size="40" :src="myUser?myUser.avatar:circleUrl"></el-avatar>
          <input class="input" type="text" v-model="content" :placeholder="'@'+name">
          <el-button type="primary" @click="submit">回复</el-button>
        </div>
      </div>
      <!-- 回复框 -->

    </div>

  </div>
</template>

<script>
import { insert } from '@/api/comment'
export default {
  props: {
    list: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      isShow: false,
      circleUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
      content: '',
      myUser: {},
      replyUser: {},
      name: '',
      commentId: ''
    }
  },
  mounted() {
    this.myUser = this.$store.state.user.profile
  },
  methods: {
    shoReply(faterInfo, info) {
      if (info) {
        this.name = info.user?.username ? info.user.username : '神秘用户'
        this.replyUser = info.user
      } else {
        this.name = faterInfo.user?.username ? faterInfo.user.username : '神秘用户'
        this.replyUser = faterInfo.user
      }
      this.commentId = faterInfo.commentId
      this.isShow = !this.isShow
    },
    submit() {
      const post_Id = this.$route.params.id
      if (this.content.trim() === '') {
        return
      }
      const postId = this.$route.params.id
      const userId = this.myUser.userId
      const data = {
        postId,
        userId,
        content: this.content,
        originId: this.commentId,
        pid: this.replyUser.userId
      }
      insert(data).then(res => {
        if (res.data) {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.content = ''
          this.$store.dispatch('moment/getCommentListFn', post_Id)
          this.isShow = false
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.mainBox {
  padding: 20px;
  border-bottom: 1px solid #c9ccd0;
  .avatar {
    display: flex;
  }
  .right {
    flex: 1;
    padding-left: 10px;
    .name {
      color: #fb7299;
      font-size: 16px;
      // padding-top: 5px;
      font-weight: 700;
      display: flex;
      // justify-content: center;
      height: 24px;

      // .name_1 {
      //   padding-top: 4px;
      // }
    }
    .content {
      padding-top: 10px;
      font-size: 15px;
      color: black;
    }
    .footer {
      padding-top: 5px;

      .time {
        font-size: 13px;
        color: #9499a0;
        .reply {
          margin-left: 10px;
          cursor: pointer;
          &:hover {
            color: #409eff;
          }
        }
      }
    }
    .sonComment {
      padding-top: 5px;
      display: flex;
      // align-items: center;
      .right {
        .name_1 {
          color: #fb7299;
          font-size: 14px;
          font-weight: 700;
          height: 24px;
          line-height: 24px;
          // padding: 0 10px;
          .huifu_text {
            color: black;
            .ai_name {
              color: #008ac5;
              cursor: pointer;
              &:hover {
                color: #409eff;
              }
            }
          }
          .footer {
            padding-top: 5px;

            .time {
              font-size: 13px;
              color: #9499a0;
              .reply {
                margin-left: 10px;
                cursor: pointer;
                &:hover {
                  color: #409eff;
                }
              }
            }
          }
        }
      }
    }
  }
  .callReply {
    padding-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    .input {
      flex: 1;
      margin: 0 5px 0 10px;
      height: 40px;
      padding-left: 10px;
      border: transparent;
      background-color: #f1f2f3;
      outline-color: transparent;
      border-radius: 8px;
      &:focus {
        background-color: #fff;
        outline-color: #c9ccd0;
        outline-width: 1px;
      }
    }
  }
}
</style>